package pers.huangyuhui.sms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.huangyuhui.sms.model.Paging;
import pers.huangyuhui.sms.model.StudentInfo;
import pers.huangyuhui.sms.service.StudentService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("studentController")
public class StudentController {

    // 获取 studentService 的 bean 对象,并使用注解进行装配
    @Resource(name = "studentService")
    private StudentService studentService;

    /**
     * @param currentPage 当前页
     * @param pageSize 分页长度
     * @param name 学生姓名
     * @param classId 班级{@code id},{@code String}类型
     * @param httpSession {@code HttpSession}类型的session作用域
     * @return 获取学生列表,并返回json类型的数据
     */
    @PostMapping("/getStudentList.do")
    @ResponseBody
    private Map<String , Object> getStudentList(
            @RequestParam(value = "page" , required = false , defaultValue = "1") Integer currentPage,
            @RequestParam(value = "rows" , required = false , defaultValue = "999") Integer pageSize,
            @RequestParam(value = "studentName" , required = false , defaultValue = "") String name,
            @RequestParam(value = "clazzid" , required = false , defaultValue = "0") Integer classId,
            HttpSession httpSession){

        //获取用户权限
        Integer userType = (Integer) httpSession.getAttribute("userType");
//        System.out.println(userType);

        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setId(0);
        studentInfo.setName(name);
        studentInfo.setClassID(classId);

        // 用户权限设置: 如果当前用户类型为学生,则将其权限设置为仅能查询个人信息
        if (userType == 2) {
            StudentInfo currentStudentInfo = (StudentInfo) httpSession.getAttribute("userInfo");// 获取当前登录的学生信息
            studentInfo.setId(currentStudentInfo.getId());
        }

        // 获取 分页后的学生列表信息
        Paging paging = new Paging(currentPage, pageSize);
        Map<String , Object> map = new HashMap<>();
        map.put("name" , studentInfo.getName());
        map.put("classID" , studentInfo.getClassID());
        map.put("id" , studentInfo.getId());
        map.put("start" , paging.getPageStart());
        map.put("size" , paging.getPageSize());
        List<StudentInfo> studentList = studentService.getStudentList(map);

        //获取 学生信息列表 条数
        int total = studentService.getStudentList(map).size();

        // 将List转化为JSON数据
        Map<String, Object> datas = new HashMap<String, Object>();
        datas.put("total", total);
        datas.put("rows", studentList);
        return datas;
    }

    /**
     * @param classId 该生所在的班级{@code id},{@code Integer}类型
     * @param sno 该生学号{@code sno},{@code String}类型
     * @param name 该生姓名{@code name},{@code String}类型
     * @param password 该生密码{@code password},{@code String}类型
     * @param sex 该生性别{@code sex},{@code String}类型
     * @param email 该生电子邮箱{@code email},{@code String}类型
     * @param phone 该生电话号码{@code phone},{@code String}类型
     * @return 如果添加成功将返回"success",否则"failure"
     * @Description： 添加学生信息的方法,且使用笨办法依次接受数据
     */
    @PostMapping("/addStudent.do")
    @ResponseBody
    private String addStudent(
            @RequestParam(value = "clazzid" , required = false , defaultValue = "0") Integer classId,
            @RequestParam(value = "sno" , required = false) String sno,
            @RequestParam(value = "name" , required = false) String name,
            @RequestParam(value = "password" , required = false) String password,
            @RequestParam(value = "sex" , required = false , defaultValue = "") String sex,
            @RequestParam(value = "email" , required = false , defaultValue = "") String email,
            @RequestParam(value = "phone" , required = false , defaultValue = "") String phone){

        // 获取添加的学生信息
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setClassID(classId);
        studentInfo.setSno(sno);
        studentInfo.setName(name);
        studentInfo.setPassword(password);
        studentInfo.setSex(sex);
        studentInfo.setEmail(email);
        studentInfo.setMobile(phone);

        try {
            // 添加学生信息
            boolean result = studentService.addStudent(studentInfo);

            // 返回操作结果
            return result ? "success" : "failure";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * @param classID 该生所在的班级{@code id},{@code Integer}类型
     * @param id 该生在数据库中的编号{@code id},{@code Integer}类型
     * @param name 该生姓名{@code name},{@code String}类型
     * @param sex 该生性别{@code sex},{@code String}类型
     * @param email 该生电子邮箱{@code email},{@code String}类型
     * @param phone 该生电话号码{@code phone},{@code String}类型
     * @return 如果添加成功将返回"success",否则"failure"
     */
    @PostMapping("/editStudent.do")
    @ResponseBody
    private String editStudent(
            @RequestParam(value = "name" , required = false) String name,
            @RequestParam(value = "sex" , required = false) String sex,
            @RequestParam(value = "email" , required = false) String email,
            @RequestParam(value = "phone" , required = false) String phone,
            @RequestParam(value = "id" , required = false) Integer id,
            @RequestParam(value = "clazzid" , required = false , defaultValue = "0") Integer classID){

        // 存储学生信息
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setId(id);
        studentInfo.setName(name);
        studentInfo.setSex(sex);
        studentInfo.setEmail(email);
        studentInfo.setMobile(phone);
        studentInfo.setClassID(classID);

        try {
            boolean result = studentService.editStudentInfo(studentInfo);
            return result ? "success" : "failure";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * @param ids 由将要被删除的学生的{@code id}组成的数组
     * @return 删除成功返回"success",失败返回"failure",错误返回"error"
     */
    @PostMapping("/deleteStudent.do")
    @ResponseBody
    private String deleteStudent(@RequestParam(value = "ids[]") String[] ids){
//        System.out.println("这个是一些值：" + studentService.toString() + Arrays.toString(ids));
        try {
            if (studentService.deleteStudent(ids)) {
                return "success";
            } else {
                return "failure";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}
